﻿namespace Sidvall.UI.Entities
{
    public sealed class TimeItem : BusinessBase
    {
        #region Public Members

        #region Id

        private string _Id;
        public string Id
        {
            get
            {
                return _Id;
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (_Id == value)
                    return;
                _Id = value;
                OnPropertyChanged(nameof(Id), true, true);
            }
        }

        #endregion
        #region TypeId

        private string _TypeId;
        public string TypeId
        {
            get
            {
                return _TypeId;
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (_TypeId == value)
                    return;
                _TypeId = value;
                OnPropertyChanged(nameof(TypeId), true, true);
            }
        }

        #endregion
        #region Name

        private string _Name = string.Empty;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (_Name == value)
                    return;
                _Name = value;
                OnPropertyChanged(nameof(Name), true, true);
            }
        }

        #endregion
        #region Time

        private System.DateTime _Time = System.DateTime.UtcNow;
        public System.DateTime Time
        {
            get
            {
                return Sidvall.Business.ValidationManager.GetValidDateTime(_Time);
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidUniversalDateTime(value, System.DateTimeKind.Utc);
                if (_Time == value)
                    return;
                _Time = value;
                OnPropertyChanged(nameof(Time), true, true);
            }
        }

        #endregion
        #region DataItem

        private object _DataItem;
        public object DataItem
        {
            get
            {
                return _DataItem;
            }
            set
            {
                _DataItem = value;
                OnPropertyChanged(nameof(DataItem), true, false);
            }
        }

        #endregion
        #region DataItemContext

        private string _DataItemContext;
        public string DataItemContext
        {
            get
            {
                return _DataItemContext;
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (_DataItemContext == value)
                    return;
                _DataItemContext = value;
                OnPropertyChanged(nameof(DataItemContext), true, true);
            }
        }

        #endregion

        #region AcceptChanges

        public void AcceptChanges()
        {
            AcceptChanges(null);
        }
        public void AcceptChanges(Sidvall.Data.AcceptChangesSettings acceptChangesSettings)
        {
            if (this.DataRowStateIsDirty)
            {
                if (acceptChangesSettings == null)
                    acceptChangesSettings = new Sidvall.Data.AcceptChangesSettings();
                Sidvall.Data.EntityManager.AcceptItemChanges(this, acceptChangesSettings);
            }
        }

        #endregion
        #region Copy

        public TimeItem Copy()
        {
            return Copy(null);
        }
        public TimeItem Copy(Sidvall.Data.CopySettings copySettings)
        {
            var item = new TimeItem();
            item.Import(this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(TimeItem item)
        {
            Import(item, null);
        }
        public void Import(TimeItem item, Sidvall.Data.CopySettings copySettings)
        {
            if (item == null)
                return;
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (copySettings.FieldCopyOption != Business.FieldCopyOptions.None)
            {
                _Id = item.Id;
                _TypeId = item.TypeId;
                _Name = item.Name;
                _Time = item.Time.ToUniversalTime();
                _DataItem = item.DataItem;
                _DataItemContext = item.DataItemContext;
            }
            Sidvall.Data.EntityManager.AfterImportItem(this, item, copySettings);
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return this.Name;
        }

        #endregion

        #endregion
        #region Constructors

        public TimeItem()
        {
        }

        #endregion
    }
}
